package 柠檬水找零;

class Solution {
    public boolean lemonadeChange(int[] bills) {
        int five = 0, ten = 0, n = bills.length;
        for (int i = 0; i < n; i++) {
            int t = bills[i];
            if (t == 5) {
                five++;
            } else if (t == 10) {
                if (--five < 0) {
                    return false;
                }
                ten++;
            } else if (t == 20) {
                if (ten - 1 >= 0 && five - 1 >= 0) {
                    ten--;
                    five--;
                } else if (five - 3 >= 0) {
                    five -= 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}
